#!/usr/bin/env python
# -*- coding: utf-8 -*-

from optparse import OptionParser

import httplib2

def update_resource(uri, path, verbose=False):
    h = httplib2.Http()
    body = file(path, 'rb').read()
    r, c = h.request(uri, method='PUT', body=body,
                     headers={'content-type': 'application/atom+xml;type=entry'})

    if verbose:
        print r
        print c

        
def parse_command_line():
    parser = OptionParser()
    parser.add_option("-u", "--uri", dest="uri",
                      help="uri of the resource to get")
    parser.add_option("-f", "--filepath", dest="filepath",
                      help="path to the file containing the resource to be updated")
    parser.add_option("-v", "--verbose", action="store_true", default=False,
                      help="if used will display the response sent by the server")

    (options, args) = parser.parse_args()

    return options.uri, options.filepath, options.verbose

if __name__ == '__main__':
    uri, filepath, verbose = parse_command_line()
    if uri is None:
        print "ABORTED: You must provide a URI"
    elif filepath is None:
        print "ABORTED: You must provide the path to the resource"
    else:
        update_resource(uri, filepath, verbose)
